import 'package:flutter/material.dart';
import 'package:intl/intl.dart';
import 'package:kxy_flutter_app/model/exam.dart';
import 'package:kxy_flutter_app/view/examination/do_exam_view.dart';

class SpecExamWidget extends StatelessWidget {
  final List<Exam> examList;
  final Function refresh;
  const SpecExamWidget({
    Key key,
    @required this.examList,
    @required this.refresh,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Column(
      children: examList.map((exam) {
        return examItemWidget(exam, context);
      }).toList(),
    );
  }

  Widget examItemWidget(Exam exam, BuildContext context) {
    return Container(
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.circular(5),
        boxShadow: [
          BoxShadow(
            color: Color.fromRGBO(240, 240, 240, 1),
            offset: Offset(1, 1),
          ),
          BoxShadow(
            color: Color.fromRGBO(240, 240, 240, 1),
            offset: Offset(-1, -1),
          ),
        ],
      ),
      padding: EdgeInsets.symmetric(vertical: 10, horizontal: 20),
      margin: EdgeInsets.symmetric(horizontal: 10, vertical: 10),
      child: Column(
        children: [
          Container(
            padding: EdgeInsets.only(top: 5),
            child: Row(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                scoreWidget(exam.score),
                titleWidget(exam.name),
              ],
            ),
          ),
          Container(
            padding: EdgeInsets.only(top: 10),
            margin: EdgeInsets.only(top: 10),
            decoration: BoxDecoration(
              border: Border(
                top: BorderSide(width: 1, color: Color.fromRGBO(240, 240, 240, 1)),
              ),
            ),
            child: infoWidget(exam, context),
          ),
        ],
      ),
    );
  }

  Widget infoWidget(Exam exam, BuildContext context) {
    var format = new DateFormat('MM月dd日');
    return Row(
      mainAxisAlignment: MainAxisAlignment.spaceBetween,
      children: [
        Row(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Text(
              '${format.format(exam.startTime)} 至 ${format.format(exam.endTime)}',
              style: TextStyle(
                fontSize: 12,
                color: Colors.black54,
              ),
            ),
            Container(width: 5),
            Text(
              '共${exam.quesCount.toString()}题',
              style: TextStyle(
                fontSize: 12,
                color: Colors.black54,
              ),
            ),
          ],
        ),
        btnExam(exam, context),
      ],
    );
  }

  Widget btnExam(Exam exam, BuildContext context) {
    return GestureDetector(
      child: Container(
        padding: EdgeInsets.symmetric(horizontal: 15, vertical: 10),
        decoration: BoxDecoration(
          borderRadius: BorderRadius.circular(50),
          color: exam.status == ExamStatus.Started ? Color.fromRGBO(255, 185, 72, 1) : Colors.black12,
        ),
        child: Text(
          '开始答题',
          style: TextStyle(
            fontSize: 12,
            color: Colors.white,
          ),
        ),
      ),
      onTap: () async {
        if (exam.status != ExamStatus.Started) return;
        var confirm = await confirmExam(context);
        if (confirm == null || !confirm) return;

        await Navigator.push(
          context,
          MaterialPageRoute(
            builder: (cxt) => DoExamView(exam: exam),
          ),
        );
        refresh();
      },
    );
  }

  Widget titleWidget(String title) {
    return Text(
      title,
      style: TextStyle(
        fontSize: 16,
        color: Colors.black87,
      ),
      maxLines: 2,
      overflow: TextOverflow.ellipsis,
    );
  }

  Widget scoreWidget(double score) {
    return Container(
      width: 90,
      alignment: Alignment.center,
      padding: EdgeInsets.only(right: 20),
      margin: EdgeInsets.only(right: 20),
      decoration: BoxDecoration(
        border: Border(
          right: BorderSide(color: Colors.black12),
        ),
      ),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
          Text(
            '得分',
            style: TextStyle(
              fontSize: 14,
              color: Colors.black54,
            ),
          ),
          Padding(
            padding: EdgeInsets.only(top: 5),
            child: Text(
              score >= 0 ? score.toString() : '未考试',
              maxLines: 1,
              overflow: TextOverflow.ellipsis,
              style: TextStyle(
                fontSize: 16,
                color: Colors.black54,
              ),
            ),
          ),
        ],
      ),
    );
  }

  Future<bool> confirmExam(BuildContext context) async {
    return showDialog(
      context: context,
      builder: (cxt) {
        return AlertDialog(title: Text('提示'), content: Text('您确认进入本场考试吗？'), actions: [
          FlatButton(
            child: Text('取消'),
            onPressed: () {
              Navigator.pop(context, false);
            },
          ),
          FlatButton(
            child: Text('确认'),
            onPressed: () {
              Navigator.pop(context, true);
            },
          ),
        ]);
      },
    );
  }
}
